#include "dspSimulator/instruction/ADDK.h"

namespace dspsimulator {
    void ADDK::generate_ir(const std::shared_ptr<simulator::IRGenElement> &ir_gen_element) {
        std::shared_ptr<simulator::LLVMJit> llvm_jit = ir_gen_element->m_llvm_jit;
        RegIRCopy &reg_ir_copy = ir_gen_element->m_reg_ir_copy;
        std::shared_ptr<llvm::IRBuilder<>> ir_builder = llvm_jit->ir_builder();
        // ADDK .func imm32,src2_reg,dst_reg
        auto op1 = std::dynamic_pointer_cast<Immediate32>(m_op1);
        auto op2 = std::dynamic_pointer_cast<SingleReg>(m_op2);

        if (op1 && op2) {
            u32 src1 = op1->data();
            std::string dst_name = get_reg_name(op2->reg());
            llvm::Value *src2 = reg_ir_copy[dst_name];
            llvm::Value *dst_ptr = IRGenerator::get_global_value_ptr(llvm_jit, dst_name);
            llvm::Value *result = ir_builder->CreateAdd(ir_builder->getInt32(src1), src2);
            ir_builder->CreateStore(result, dst_ptr);
        }
    }

    std::shared_ptr<DspInstruction>
    ADDK::decode_standard_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<ADDK> addk;
        std::bitset<32> &data = dsp_instruction_info->m_standard_bits;
        if (common::check_bits<5>(data, 0b10100, 2) &&
            exist_standard_condition(common::extract_bits<4>(data, 28))) {
            // ADDK .S scst16,uint
            addk = std::make_shared<ADDK>(dsp_instruction_info->address(), 4);
            u32 cst16 = common::extract_bits<16>(data, 7).to_ulong();
            u32 dst = common::extract_bits<5>(data, 23).to_ulong();
            bool s = data[1];
            bool x = data[12];
            addk->m_p = data[0];
            addk->m_creg_z = common::extract_bits<4>(data, 28);
            addk->m_func_unit = s ? FuncUnit::S1 : FuncUnit::S2;
            addk->m_op1 = std::make_shared<Immediate32>(common::extract_bit_to_int<16>(cst16));
            addk->m_op2 = std::make_shared<SingleReg>(get_dst_reg32_kind(dst, s));
        }
        return addk;
    }

    std::shared_ptr<DspInstruction>
    ADDK::decode_compact_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<ADDK> addk;
        std::bitset<16> &data = dsp_instruction_info->m_compact_bits;
        if (data[10] & common::check_bits<6>(data, 0b010111, 1)) {
            addk = std::make_shared<ADDK>(dsp_instruction_info->address(), 2);
            u32 ucst5 = common::extract_bits<2>(data, 11).to_ulong() << 3;
            ucst5 = common::extract_bits<3>(data, 13).to_ulong();
            u32 dst = common::extract_bits<3>(data, 7).to_ulong();
            bool s = data[0];
            addk->m_p = dsp_instruction_info->p();
            addk->m_func_unit = s ? FuncUnit::S1 : FuncUnit::S2;
            addk->m_op1 = std::make_shared<Immediate32>(ucst5);
            addk->m_op2 = std::make_shared<SingleReg>(get_dst_reg32_kind(dst, s));
        }
        return addk;
    }

    std::string ADDK::to_string() const {
        return get_standard_condition(m_creg_z) + m_name + " " + get_function_unit_name(m_func_unit) + " " +
               m_op1->to_string() + "," + m_op2->to_string();
    }
}